<?php

namespace app\admin\controller\subject;

// 引入基类控制器
use app\common\controller\Backend;

class Subrecyclebin extends Backend
{
    // 设置关联查询
    protected $relationSearch = true;

    // 当前模型
    protected $model = null;

    // 无需登陆
    protected $noNeedLogin = [];
    // 无需鉴权
    protected $noNeedRight = [];

    public function __construct(){
        parent::__construct();

        $this->model = model('Subject.Subject');
        $this->CategoryModel = model('Subject.Category');
        $this->ChapterModel = model('Subject.Chapter');
    }

    public function index(){
        // 接收请求，查询数据
        // 判断是否有ajax请求

        //将请求当中所有的参数去除html标签，去掉两边空白
        $this->request->filter(['strip_tags', 'trim']);

        if($this->request->isAjax()){
            //获取表格所提交过来筛选和排序的一些参数
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            // 表格需要两个返回值参数 总条数和分页的数据
            $total = $this->model
                ->onlyTrashed()
                ->with(['category'])
                ->where($where)
                ->order($sort,$order)
                ->count(); //返回查询的总数

            // var_dump($total);
            // exit;
            $list = $this->model
                ->onlyTrashed()
                ->with(['category'])
                ->where($where)
                ->order($sort,$order)
                ->limit($offset,$limit)
                ->select();
            // var_dump(collection($list)->toArray());
            // exit;

            // 打印sql语句
            // echo $this->model->getLastSql();
            // exit;

            // 组装数据
            $result = [
                'total' => $total,
                'rows' => $list
            ];
            // 返回给ajax的数据
            return json($result);  

        }
        return $this->view->fetch();
    }
    public function destroy($ids=NULL){

        $ids = $ids ?: $this->request->param('ids','','trim');
        $rows = $this->model->onlyTrashed()->select($ids);
        // var_dump($rows);
        // exit;

        // 没有数据
        if(!$rows){
            $this->error(__('No Results were found'));
            exit;
        }

        // 删除图片
        // 从数据中提取出thumbs这个字段的值
        $thumbs = array_column($rows,"thumbs");

        // 去重、去空
        $thumbs = array_filter($thumbs);
        $thumbs = array_unique($thumbs);

        // 查找章节
        $chapter = $this->ChapterModel->where(['subid' => ['IN',$ids]])->select();
        // var_dump(collection($chapter)->toArray());
        // exit;
        $url = array_column($chapter,"url");
        // var_dump($url);
        // exit;
        $url = array_filter($url);
        $url = array_unique($url);

        // 销毁
        $result = $this->model->onlyTrashed()->where(['id' => ['IN',$ids]])->delete(true);

        if($result === FALSE){
            $this->error("销毁失败");
            exit;
        }else{
            // 删除图片
            if(!empty($thumbs)){
                foreach($thumbs as $item){
                    is_file(".".$item) && @unlink(".".$item);
                }
            }

            // 删除视频
            if(!empty($url)){
                foreach($url as $item){
                    is_file(".".$item) && @unlink(".".$item);
                }
            }

            $this->success("销毁成功");
            exit;
        }
    }
        
    public function restore($ids=NULL){
        $ids = $ids ?: $this->request->param('ids','','trim');
        $rows = $this->model->onlyTrashed()->select($ids);
        // var_dump($rows);
        // exit;

        // 没有数据
        if(!$rows){
            $this->error(__('No Results were found'));
            exit;
        }

        $result = $this->model->onlyTrashed()->where(['id' => ['IN',$ids]])->update(['deletetime'=>NULL]);

        if($result === FALSE){
            $this->error('恢复数据失败');
            exit;
        }else{
            $this->success('恢复数据成功');
            exit;
        }


    }

}
